//
//  CustomerControlView.m
//  网络视频播放多段缓存
//
//  Created by apple on 16/5/26.
//  Copyright © 2016年 cheniue. All rights reserved.
//

#import "CustomerControlView.h"
#import "CustomerAVPlayerView.h"

@implementation CustomerControlView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _onControl = NO;
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, self.bounds.size.width, 64.0f)];
        [_topView setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.8f]];
        [self addSubview:_topView];
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton setFrame:CGRectMake(0.0f, 22.0f, 40.0f, 40.0f)];
        [_backButton setImage:[UIImage imageNamed:@"navigation_back_icon"] forState:UIControlStateNormal];
            [_topView addSubview:_backButton];
//        _suxiaButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_suxiaButton setFrame:CGRectMake(_topView.bounds.size.width-110.0f, 22.0f, 40.0f, 40.0f)];
//        [_suxiaButton setImage:[UIImage imageNamed:@"navigation_back_icon"] forState:UIControlStateNormal];
//        [_topView addSubview:_suxiaButton];

        
//        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(48.0f, 20.0f, _topView.bounds.size.width-150.0f, 44.0f)];
//        [_titleLabel setTextColor:[UIColor lightGrayColor]];
//        [_titleLabel setFont:[UIFont systemFontOfSize:14]];
//        [_titleLabel setBackgroundColor:[UIColor clearColor]];
//        [_topView addSubview:_titleLabel];
        
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0.0f, self.bounds.size.height-64.0f, self.bounds.size.width, 64.0f)];
        [_bottomView setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.8f]];
        [self addSubview:_bottomView];
        
        _progressSlider = [[CustomerProgressSlider alloc]initWithFrame:CGRectMake(0.0f, 0.0f, self.bounds.size.width, 35.0f)];
        _progressSlider.userInteractionEnabled = NO;

        [_bottomView addSubview:_progressSlider];
        
        _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playButton setFrame:CGRectMake(10.0f, 22.0f, 40.0f, 40.0f)];
        [self setPlayButtonSelected:NO];
        [_bottomView addSubview:_playButton];
        
        _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(68.0f, 22.0f, _bottomView.bounds.size.width-118.0f, 40.0f)];
        [_timeLabel setBackgroundColor:[UIColor clearColor]];
        [_timeLabel setTextColor:[UIColor lightGrayColor]];
        [_timeLabel setFont:[UIFont systemFontOfSize:12]];
        [_bottomView addSubview:_timeLabel];
        
//        _scaleButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_scaleButton setFrame:CGRectMake(_bottomView.bounds.size.width-50.0f, 22.0f, 40.0f, 40.0f)];
//        [_scaleButton setImage:[UIImage imageNamed:@"video-player-fullscreen"] forState:UIControlStateNormal];
//        [_bottomView addSubview:_scaleButton];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    [_topView setFrame:CGRectMake(0.0f, 0.0f, self.bounds.size.width, 64.0f)];
    [_backButton setFrame:CGRectMake(0.0f, 22.0f, 40.0f, 40.0f)];
//    [_suxiaButton setFrame:CGRectMake(_topView.bounds.size.width- 110, 22.0f, 40.0f, 40.0f)];

//    [_titleLabel setFrame:CGRectMake(48.0f, 20.0f, _topView.bounds.size.width-48.0f, 44.0f)];
    [_bottomView setFrame:CGRectMake(0.0f, self.bounds.size.height-64.0f, self.bounds.size.width, 64.0f)];
    [_progressSlider setFrame:CGRectMake(0.0f, 0.0f, self.bounds.size.width, 20.0f)];
    [_playButton setFrame:CGRectMake(10.0f, 22.0f, 40.0f, 40.0f)];
    [_timeLabel setFrame:CGRectMake(68.0f, 22.0f, _bottomView.bounds.size.width-118.0f, 40.0f)];
//    [_scaleButton setFrame:CGRectMake(_bottomView.bounds.size.width-50.0f, 22.0f, 40.0f, 40.0f)];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    _onControl = YES;
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    _onControl = YES;
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    _onControl = NO;
    if ([self.superview isKindOfClass:[CustomerAVPlayerView class]])
    {
        [(CustomerAVPlayerView*)self.superview performSelector:@selector(dissmissControlView) withObject:nil afterDelay:2.0f];
    }
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    _onControl = NO;
    if ([self.superview isKindOfClass:[CustomerAVPlayerView class]])
    {
        [(CustomerAVPlayerView*)self.superview performSelector:@selector(dissmissControlView) withObject:nil afterDelay:2.0f];
    }
}

-(void)setPlayButtonSelected:(BOOL)selected
{
    _playButton.selected = selected;
    if (_playButton.selected)
    {
        [_playButton setImage:[UIImage imageNamed:@"video-player-pause"] forState:UIControlStateNormal];
    }
    else
    {
        [_playButton setImage:[UIImage imageNamed:@"video-player-play"] forState:UIControlStateNormal];
    }
}
@end
